#include "stdafx.h"
#include "ckDataSourceManagerAccess.h"
#include "ckDataSource.h"

namespace ck
{
	DataSourceManagerAccess::DataSourceManagerAccess()
	{
	}


	DataSourceManagerAccess::~DataSourceManagerAccess()
	{
	}

	void DataSourceManagerAccess::BuildCreateDataSourceTableSQL(char* szSQL, size_t nSize)
	{
		const char* format = "CREATE TABLE %s (ID AUTOINCREMENT, CK_PROJ_CODE INT, CK_NAME VARCHAR(255), CK_PATH VARCHAR(255), CK_TYPE INT)";
		snprintf(szSQL, nSize, format, CK_DATASOURCE_TABLE_SOURCE);
	}

	void DataSourceManagerAccess::BuildGetDataSourceSQL(char* szSQL, size_t nSize, z_uint32 nProjCode)
	{
		const char* format = "SELECT ID, CK_PROJ_CODE, CK_NAME, CK_PATH, CK_TYPE FROM %s WHERE CK_PROJ_CODE=%d";
		snprintf(szSQL, nSize, format, CK_DATASOURCE_TABLE_SOURCE, nProjCode);
	}

	void DataSourceManagerAccess::BuildInsertDataSourceSQL(char* szSQL, size_t nSize, z_uint32 nProjCode, DataSource* pDataSource)
	{
		const char* format = "INSERT INTO %s (CK_PROJ_CODE, CK_NAME, CK_PATH, CK_TYPE) VALUES(%d,'%s','%s',%d)";
		snprintf(szSQL, nSize, format, CK_DATASOURCE_TABLE_SOURCE, nProjCode, pDataSource->GetName(), pDataSource->GetPath(), pDataSource->GetType());
	}

	void DataSourceManagerAccess::BuildRemoveDataSourceSQL(char* szSQL, size_t nSize, z_uint32 nProjCode, const char* szSourceName)
	{
		const char* format = "DELETE FROM %s WHERE CK_PROJ_CODE=%d AND CK_NAME='%s'";
		snprintf(szSQL, nSize, format, CK_DATASOURCE_TABLE_SOURCE, nProjCode, szSourceName);
	}

	void DataSourceManagerAccess::BuildInsertDatasetSQL(char* szSQL, size_t nSize, z_uint32 nSourceCode, const char* szDatasetName, ckDataProductType nProductTypetype)
	{
		const char* format = "INSERT INTO %s (CK_NAME, CK_SOURCE_CODE) VALUES('%s',%d)";
		snprintf(szSQL, nSize, format, CK_DATASOURCE_TABLE_DATASET, szDatasetName, nSourceCode);
	}

	void DataSourceManagerAccess::BuildGetDataSourceCodeSQL(char* szSQL, size_t nSize, z_uint32 nProjCode, const char* szSourceName)
	{
		const char* format = "SELECT ID FROM %s WHERE CK_PROJ_CODE=%d AND CK_NAME='%s'";
		snprintf(szSQL, nSize, format, CK_DATASOURCE_TABLE_SOURCE, nProjCode, szSourceName);
	}

	void DataSourceManagerAccess::BuildGetDatasetsSQL(char* szSQL, size_t nSize, z_uint32 nSourceCode)
	{
		const char* format = "SELECT ID, CK_NAME FROM %s WHERE CK_SOURCE_CODE=%d";
		snprintf(szSQL, nSize, format, CK_DATASOURCE_TABLE_DATASET, nSourceCode);
	}

	void DataSourceManagerAccess::BuildRemoveDatasetSQL(char* szSQL, size_t nSize, z_uint32 nSourceCode, const char* szDatasetName)
	{
		const char* format = "DELETE FROM %s WHERE CK_SOURCE_CODE=%d AND CK_NAME='%s'";
		snprintf(szSQL, nSize, format, CK_DATASOURCE_TABLE_DATASET, nSourceCode, szDatasetName);
	}

	void DataSourceManagerAccess::BuildRemoveDatasetsSQL(char* szSQL, size_t nSize, z_uint32 nSourceCode)
	{
		const char* format = "DELETE FROM %s WHERE CK_SOURCE_CODE=%d";
		snprintf(szSQL, nSize, format, CK_DATASOURCE_TABLE_DATASET, nSourceCode);
	}
}
